package com.bc.commonentity.common;


import com.bc.commoncore.util.HttpUtil;
import com.bc.commoncore.util.JsonUtil;
import com.bc.commonentity.enums.HttpReturnRnums;

import java.util.ArrayList;
import java.util.List;

public class ApiHttpUtil
{
	/**
	 * 获取JsonResult对象
	 * @param url
	 * @param param
	 * @author zhaohailong
	 * @date 20160726
	 * @return
	 */
	public static JsonResult getJsonResult(String url, Object param) {
		String paramJson="";
		if (param!=null){
			if(param instanceof String) {
				paramJson=param.toString();
			}else{
				paramJson=JsonUtil.obj2string(param);
			}
		}
			
		String result = HttpUtil.sendJsonPost(url,paramJson);
		return JsonUtil.str2obj(result, JsonResult.class);
	}
	/**
	 * 获取JsonResult对象中的Data对象
	 * @param url
	 * @param param
	 * @param type
	 * @author zhaohailong
	 * @date 20160726
	 * @return
	 */
	public static <T> T getResult(String url, Object param,Class<T> type) {
		JsonResult resultModel = getJsonResult(url,param);
		if (resultModel.getCode() != HttpReturnRnums.Success.value()) {
			String msg=resultModel.getMsg()+":"+(resultModel.getData()==null?"":resultModel.getData().toString());
			throw new RuntimeException(url+":"+msg);
		}
		if (resultModel.getData() != null) {
			if (isBaseObj(type)) {
				if(isBaseObj(resultModel.getData().getClass())){
					return (T)resultModel.getData();
				}else {
					return (T)JsonUtil.obj2string(resultModel.getData());
				}
				
			}else {
				if(isBaseObj(resultModel.getData().getClass())){
					throw new RuntimeException(resultModel.getData().getClass().getName()+"不能转换为"+type.getName());
				}
				String data=JsonUtil.obj2string(resultModel.getData());
				return JsonUtil.str2obj(data,type);
			}

		}
		return null;
	}
	private static boolean isBaseObj(Class type){
		if (type==int.class||
				type==Integer.class||
				type==double.class||
				type==Double.class||
				type==float.class||
				type==Float.class||
				type==long.class||
				type==Long.class||
				type==short.class||
				type==Short.class||
				type==boolean.class||
				type==Boolean.class||
				type==byte.class||
				type==Byte.class||
				type==char.class||
				type==Character.class||
				type==String.class) {
			return true;
		}
		return false;
	}
	/**
	 * 获取JsonResult对象中的Data(PagedResponse)对象
	 * @param url
	 * @param param
	 * @param type
	 * @author zhaohailong
	 * @date 20160726
	 * @return
	 */
	public static PagedResponse getPagedResult(String url, Object param,Class type) {
		PagedResponse pagedResponse=getResult(url,param,PagedResponse.class);
		if (pagedResponse!=null) {
			if (pagedResponse.getResultList()!=null&&pagedResponse.getResultList().size()>0) {
				String pagedData=JsonUtil.obj2string(pagedResponse.getResultList());
				List listData=JsonUtil.str2list(pagedData,type);
				pagedResponse.setResultList(listData);
			}else if(pagedResponse.getResultList()==null){
				pagedResponse.setResultList(new ArrayList());
			}

			return pagedResponse;
		}
		return null;
	}
	/**
	 * 获取JsonResult对象中的Data(List)对象
	 * @param url
	 * @param param
	 * @param type
	 * @author zhaohailong
	 * @date 20160726
	 * @return
	 */
	public static List getListResult(String url, Object param,Class type) {
		String listData=getResult(url,param,String.class);
		if (listData!=null&&!listData.isEmpty()) {
			return JsonUtil.str2list(listData,type);
		}
		return new ArrayList();
	}
}
